#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int closetTarget(vector<string>& words, string target, int startIndex) {
        if (words[startIndex] == target)
            return 0;
        int n = words.size();
        int initial = startIndex;
        unsigned int min = -1;
        int count = 1;
        int i = 0;
        for (i = (initial + 1) % n; i != initial;)
        {
            if (target == words[i])
            {
                min = (min > count) ? count : min;
                break;
            }
            ++count;
            i = (i + 1) % n;
        }
        count = 1;
        for (i = (initial - 1 + n) % n; i != initial;)
        {
            if (target == words[i])
            {
                min = (min > count) ? count : min;
                break;
            }
            ++count;
            i = (i - 1 + n) % n;
        }

        return min == 0 ? -1 : min;
    }
};